package com.tywj.buscustomerapp.view.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.tywj.buscustomerapp.R;

public class TaylorAutoViewPager extends ViewPager {
    private final int default_duration = 5000;

    private int duration = default_duration;
    /**
     * 是否自动滚动
     */
    private boolean autoScroll = true;
    /**
     * 是否循环滚动
     */
    private boolean indeterminate = true;

    private float startX = 0;

    public TaylorAutoViewPager(Context context) {
        this(context, null);
    }

    public TaylorAutoViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoScrollViewPager);
        duration = typedArray.getInt(R.styleable.AutoScrollViewPager_duration, default_duration);
        setAutoScrollEnabled(typedArray.getBoolean(R.styleable.AutoScrollViewPager_autoScroll, true));
        indeterminate = typedArray.getBoolean(R.styleable.AutoScrollViewPager_indeterminate, true);

        typedArray.recycle();
    }

    private Runnable autoScrollRunnable = new Runnable() {
        @Override
        public void run() {
            if (getAdapter()!=null && getCurrentItem() == getAdapter().getCount() - 1) {
//                currentItem = 0
                setCurrentItem(0, true);
            } else {
                setCurrentItem(getCurrentItem() + 1);
            }
            postDelayed(autoScrollRunnable, duration);
        }
    };

    public void setAutoScrollEnabled(boolean value) {
        autoScroll = value;
        if (autoScroll) {
            startAutoScroll();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setAutoScrollEnabled(autoScroll);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAutoScroll();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {
            setAutoScrollEnabled(autoScroll);
        } else {
            stopAutoScroll();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN: {
                    //按下手指停止自动滚动
                    stopAutoScroll();
                    startX = ev.getX();
                    break;
                }
            }
            return super.onInterceptTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        try {
            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_UP: {
                    //如果indeterminate为true则手指滑动到最后一个时再滑动则跳到一个view
                    if (indeterminate
                            && (getCurrentItem() == 0
                            || getCurrentItem() == getAdapter().getCount() - 1)) {

                        if (getCurrentItem() == getAdapter().getCount() - 1
                                && (ev.getX() < startX && startX - ev.getX() > 50)) {
                            postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    setCurrentItem(0, true);
                                }
                            }, 200);
                        }
                    }
                    //移开手指开始自动滚动
                    startAutoScroll();
                    break;
                }
            }
            return super.onTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    private void startAutoScroll() {
        stopAutoScroll();
        postDelayed(autoScrollRunnable, duration);
    }

    private void stopAutoScroll() {
        removeCallbacks(autoScrollRunnable);
    }
}

